﻿using Microsoft.Kinect;
using System.Diagnostics;
using System.Threading;

namespace Fizbin.Kinect.Gestures.Segments
{

    /**
     * Definicoes dos segmentos do movimento hadouken
     * O movimento e composto de quatro segmentos que sao descritos abaixo
     * O primeiro e segundo movimento sao repetidos em um looping enquando o terceiro e quarto finalizam o reconhecimento.
     * Para conferir se o segmento e valido executar o metodo GesturePartResult implementado em cada
     * um dos segmentos.
     * 
     */
    class HadoukenSegment1 : IRelativeGestureSegment
    {
        private System.Windows.Forms.TextBox tex;
        private Thread musicPlayer;
        private System.Media.SoundPlayer player;

        public HadoukenSegment1(System.Windows.Forms.TextBox tex, System.Media.SoundPlayer player)
        {
            this.tex = tex;
            this.player = player;
        }

        public GesturePartResult CheckGesture(Skeleton skeleton)
        {

            //float range = 0.1f;
            Joint leftHand = skeleton.Joints[JointType.HandLeft];
            Joint rightHand = skeleton.Joints[JointType.HandRight];
            // Right and Left Hand in front of Shoulders

            float handDX = rightHand.Position.X,
                  handDY = rightHand.Position.Y,
                  handDZ = rightHand.Position.Z;

            float handLX = leftHand.Position.X,
                  handLY = leftHand.Position.Y,
                  handLZ = leftHand.Position.Z;

            if (handDY > skeleton.Joints[JointType.ShoulderCenter].Position.Y && handLY < skeleton.Joints[JointType.Head].Position.Y && handLY > skeleton.Joints[JointType.HipCenter].Position.Y)
                {
                    player.PlayLooping();
                    return GesturePartResult.Suceed;
                }
            return GesturePartResult.Pausing;

        }
    }

    class HadoukenSegment2 : IRelativeGestureSegment
    {
        private System.Windows.Forms.TextBox tex;

        public HadoukenSegment2(System.Windows.Forms.TextBox tex)
        {
            this.tex = tex;
        }
        public GesturePartResult CheckGesture(Skeleton skeleton)
        {
            //float range = 0.1f;
            Joint leftHand = skeleton.Joints[JointType.HandLeft];
            Joint rightHand = skeleton.Joints[JointType.HandRight];


            float handDX = rightHand.Position.X,
                  handDY = rightHand.Position.Y,
                  handDZ = rightHand.Position.Z;

            float handLX = leftHand.Position.X,
                  handLY = leftHand.Position.Y,
                  handLZ = leftHand.Position.Z;

            if (handDY > skeleton.Joints[JointType.Head].Position.Y && handLY < skeleton.Joints[JointType.Spine].Position.Y)
            {
                return GesturePartResult.Suceed;
            }
            return GesturePartResult.Pausing;

        }
    }
    /**
    * Este segmento verifica se as maos estao juntas e se estao acima da espinha a uma distancia acima de 15cm
    */
    class HadoukenSegment3 : IRelativeGestureSegment
    {
        private System.Windows.Forms.TextBox tex;

        public HadoukenSegment3(System.Windows.Forms.TextBox tex)
        {
            this.tex = tex;
        }
        /// <summary>
        /// Checks the gesture.
        /// </summary>
        /// <param name="skeleton">The skeleton.</param>
        /// <returns>GesturePartResult based on if the gesture part has been completed</returns>
        public GesturePartResult CheckGesture(Skeleton skeleton)
        {
            float range = 0.1f;
            float range2 = 0.3f;
            Joint leftHand = skeleton.Joints[JointType.HandLeft];
            Joint rightHand = skeleton.Joints[JointType.HandRight];
            Joint hipCenter = skeleton.Joints[JointType.HipCenter];

            float xMen = (leftHand.Position.X - range), xMai = (leftHand.Position.X + range),
                  yMen = (leftHand.Position.Y - range), yMai = (leftHand.Position.Y + range),
                  zMen = (leftHand.Position.Z - range), zMai = (leftHand.Position.Z + range);


            float handDX = rightHand.Position.X,
                  handDY = rightHand.Position.Y,
                  handDZ = rightHand.Position.Z;

            float hipX = hipCenter.Position.X,
                  hipY = hipCenter.Position.Y,
                  hipZ = hipCenter.Position.Z;

            if ((xMen < handDX && handDX < xMai) &&
               (yMen < handDY && handDY < yMai) &&
               (zMen < handDZ && handDZ < zMai))
            {

                if (((hipX - range2 < handDX && handDX < hipX + range2) &&
                    (hipY - range2 < handDY && handDY < hipY + range2) &&
                    (hipZ - range2 < handDZ && handDZ < hipZ + range2)))
                {
                    return GesturePartResult.Suceed;
                }
            }
            return GesturePartResult.Pausing;
        }
    }
    /**
     * Este segmento verifica se as maos estao juntas e se estao acima da espinha a uma distancia acima de 15cm
     */
    class HadoukenSegment4 : IRelativeGestureSegment
    {
        private System.Windows.Forms.TextBox tex;

        public HadoukenSegment4(System.Windows.Forms.TextBox tex)
        {
            // TODO: Complete member initialization
            this.tex = tex;
        }
        /// <summary>
        /// Checks the gesture.
        /// </summary>
        /// <param name="skeleton">The skeleton.</param>
        /// <returns>GesturePartResult based on if the gesture part has been completed</returns>
        public GesturePartResult CheckGesture(Skeleton skeleton)
        {
            float range = 0.1f;
            Joint leftHand = skeleton.Joints[JointType.HandLeft];
            Joint rightHand = skeleton.Joints[JointType.HandRight];
            // Right and Left Hand in front of Shoulders

            float xMen = (leftHand.Position.X - range), xMai = (leftHand.Position.X + range),
                  yMen = (leftHand.Position.Y - range), yMai = (leftHand.Position.Y + range),
                  zMen = (leftHand.Position.Z - range), zMai = (leftHand.Position.Z + range);


            float handDX = rightHand.Position.X,
                  handDY = rightHand.Position.Y,
                  handDZ = rightHand.Position.Z;

            if ((xMen < handDX && handDX < xMai) &&
               (yMen < handDY && handDY < yMai) &&
               (zMen < handDZ && handDZ < zMai))
            {
                if (handDY > skeleton.Joints[JointType.Spine].Position.Y + 0.15f && handDZ < skeleton.Joints[JointType.Spine].Position.Z - 0.4f)
                {
                    return GesturePartResult.Suceed;
                }
            }
            return GesturePartResult.Pausing;
        }
    }
}
